﻿namespace KinonekoSoftware.Foundation.UI.Commands
{
    public class ActionCommand : ICommandEX
    {
        public ActionCommand(Action executeHandler)
        {
            ExecuteHandler = executeHandler ?? throw new ArgumentNullException(nameof(executeHandler));
        }


        public ActionCommand(Action executeHandler, Func<bool> canExecuteHandler)
        {
            ExecuteHandler    = executeHandler    ?? throw new ArgumentNullException(nameof(executeHandler));
            CanExecuteHandler = canExecuteHandler ?? throw new ArgumentNullException(nameof(canExecuteHandler));

        }

        public bool CanExecute(object parameter) => CanExecuteHandler?.Invoke() ?? true;

        public void Execute(object parameter) => ExecuteHandler();

        public Action     ExecuteHandler    { get; }
        public Func<bool> CanExecuteHandler { get; }
        public void RaiseUpdate() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

        public event EventHandler CanExecuteChanged;
    }

}